//
//  V2Controller.swift
//  test
//
//  Created by xgp on 2017/6/14.
//  Copyright © 2017年 xgp. All rights reserved.
//

import Foundation
import UIKit
import AVKit
import AVFoundation
class VController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        setupSB()
    }
    func setupAVPlayer() {
        let url = URL(string: "http://download.3g.joy.cn/video/236/60236937/1451280942752_hd.mp4")

        playerItem = AVPlayerItem(url: url!)
        player = AVPlayer(playerItem: playerItem)
        playerLayer = AVPlayerLayer(player: player)
        playerLayer?.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer!)
        
        playerItem?.addObserver(self, forKeyPath: "status", options: .new, context: nil)

    }
    func setupSB() -> Void {
        let btn = UIButton(type: .custom)
        let url = URL(string: "http://download.3g.joy.cn/video/236/60236937/1451280942752_hd.mp4")
        self.sbplayer = SBPlayer(withurl: url!)
        self.sbplayer!.title = "这是标题"
        
        self.sbplayer!.backgroundColor = UIColor.blue
        self.sbplayer!.mode = SBLayerVideoGravity.ResizeAspectFill
        sbplayer?.frame = self.view.bounds
        self.view.addSubview(sbplayer!)

    }
    var sbplayer:SBPlayer?
    var playerItem:AVPlayerItem?
    var player:AVPlayer?
    var playerLayer:AVPlayerLayer?

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "status" {
            let itemStatus:AVPlayerStatus = AVPlayerStatus(rawValue: change?[NSKeyValueChangeKey.newKey] as! Int)!
            switch itemStatus {
            case AVPlayerStatus.unknown:
                print("AVplayer status unknown")
            case .readyToPlay:
                print("AVPlayerItemStatusReadyToPlay");
                player?.play()
            case .failed:
                print("AVPlayerItemStatusFailed");
            default:
                break
            }

        }
    }
}
